package com.symria.server.http;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;


import com.symria.server.http.bean.AdShow;
import com.symria.server.http.bean.BeanToJson;
import com.symria.server.http.bean.AdRequest;
import com.symria.server.http.bean.Response;
import com.symria.server.http.json.JSONException;
import com.symria.server.http.json.JSONObject;

public class HttpHelp {

	public static final String URL="http://www.baidu.com";
	
	public static Response postAdRequest(AdRequest r){
		return postData(r.toJsonObject().toString());
	}
	public static Response postAdShow(AdShow s){
		return postData(BeanToJson.beanToJson(s).toString());
	}
	public static Response postAdClick(AdShow c){
		return postData(BeanToJson.beanToJson(c).toString());
	}
	private static Response postData(String data){
		NameValuePair[] msg = {new NameValuePair("p", data)};
		String strURL = URL;
		String result = null;
		PostMethod post = null;
		try {
			post = new PostMethod(strURL);
			post.setRequestBody(msg);
			HttpClient httpclient = new HttpClient();
			httpclient.executeMethod(post);
			if (post.getStatusCode() == HttpStatus.SC_OK) {
				result = post.getResponseBodyAsString();
			} else {
				result = post.getStatusLine().toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			post.releaseConnection();
		}
		return parseResponse(result);
	}
	
	public static Response parseResponse(String s){
		if(s!=null){
			return jsonToResponse(parseJsonObject(s));
		}else{
			return null;
		}

	}
	private static Response jsonToResponse(JSONObject o){
		Response r = new Response();
			try {
				if(o.has("adId")){
				  r.setAdId(o.getInt("adId"));
				}
				if(o.has("adUrl")){
					r.setAdUrl(o.getString("adUrl"));
				}
				if(o.has("code")){
					r.setCode(o.getString("code"));
				}
				if(o.has("ct")){
					r.setCt("ct");
				}
				if(o.has("ftp")){
					r.setFtp(o.getString("ftp"));
				}
				if(o.has("imgPath")){
					r.setImgPath(o.getString("imgPath"));
				}
				if(o.has("playType")){
					r.setPlayType("playType");
				}
				if(o.has("text")){
					r.setText(o.getString("text"));
				}
				if(o.has("type")){
					r.setType(o.getString("type"));
				}
			} catch (JSONException e) {
				return r;
			}
		return r;
	}
	
	public static void main(String[] args){
		AdRequest r = new AdRequest();
		r.setImei("asdfsdfsdf");
		r.setSim("aekdkdldlfo");
		r.setImsi("asdfiasdfdf");
		r.setNet("10086");
		//Response res=HttpHelp.postAdRequest(r);
		Response res=HttpHelp.parseResponse("{\"adId\":\"100\"}");
		System.out.println(res.getAdId());
		//System.out.println(res.getAdUrl());
		
		//Response r = HttpHelp.parseResponse("{list:\"[]\"}");

	}
	
	public static JSONObject parseJsonObject(String str) {
		try {
			return new JSONObject(str);
		} catch (JSONException e) {
			e.printStackTrace();
			return new JSONObject();
			
		}
	}
	
}
